package com.dxy.app.widget.draw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
  *Created by duxingyu on 2017/8/4.15:50
  *Email: 
  *Description:仿qq四处观望的底部indicator
  **/

public class LookAroundView extends View {

  public static final String TAG="TAG_LookAroundView";

  public LookAroundView(Context context) {
    super(context);
    init();
  }

  public LookAroundView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public LookAroundView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
  }

  private int width,height;
  private void init(){
    paint =new Paint();
    paint.setColor(Color.RED);
    paint.setAntiAlias(true);

    move =new PointF();
    stable =new PointF(width/2,height/2);
  }

  @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width =w;
    height =h;
    stable.x =width/2;
    stable.y =height/2;
  }

  private PointF move,stable;
  private Paint paint;
  @Override protected void onDraw(Canvas canvas) {
    canvas.drawCircle(stable.x,stable.y,30,paint);
    canvas.drawCircle(move.x,move.y,100,paint);
  }


  @Override public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()){
      case MotionEvent.ACTION_DOWN:
      case MotionEvent.ACTION_MOVE:
        //Log.i(TAG, "onTouchEvent: "+"move:("+event.getRawX()+","+event.getRawX()+")");
        move.x =event.getRawX();
        move.y =event.getRawY();
        judgeCurrentQuadrand();

        invalidate();
        break;
      case MotionEvent.ACTION_UP:
        break;
    }
    return true;
  }
  private PointF stableStart,stableEnd,moveStart,moveEnd;
  private Quadrand quadrand;
  private void judgeCurrentQuadrand(){
    float delX =move.x-stable.x;
    float delY =move.y-stable.y;

    if(delX>=0&&delY<0){
      quadrand =Quadrand.FIRST;
    }else if(delX<0&&delY<=0){
      quadrand =Quadrand.SECOND;
    }else if(delX<=0&&delY>0){
      quadrand =Quadrand.THIRD;
    }else{
      quadrand =Quadrand.FOURTH;
    }

    //Log.i(TAG, "judgeCurrentQuadrand: "+quadrand.name()+","+quadrand.getDisc());
    double tanα =(move.x-stable.x)/(move.y-stable.y);

    double α =Math.atan(tanα);//获取当前两点之间形成圆的弧度,范围是[-π/2~π/2];
    double degree =Math.toDegrees(α);//将对应弧度转换成角度
    //Log.i(TAG, "onTouchEvent: "+degree);
    calculateMovePoints(degree);
  }

  /**
   * 两圆心连线与贝塞尔曲线起始坐标与圆心之间所绘制半径之间的夹角
   * */
  public static final int CONTACT_POINT_DEGREE_WITH_AXLE=60;

  /**
   * 计算相关坐标
   * @param degree 与X轴的偏移角度
   * */
  private void calculateMovePoints(double degree){
    degree =Math.abs(degree);
    Log.i(TAG, "calculateMovePoints: "+degree);

  }
}
